<?php
	class Mdeal extends CI_Model{
		public function __construct(){
			parent::__construct();
			$this->load->database();
		}
		
		public function add_deal($did_category,$dname,$dprice,$dsale_off,$dprice_sale_off,$filePath1,$filePath2,$dtitle,$dadvantage,$dcondition,$dcontent,$top_seller,$top_view,$top_100,$freeship,$delivery,$idaccount)
		{
			$result=$this->db->query("INSERT INTO `deal` (`id_account`,`id_category`, `name`, `price`, `sale_off`, `price_sale_off`, `small_image`, `large_image`, `title`, `advantage`, `condition`, `detail`, `freeship`, `delivery`, `top_seller`, `top_view`, `top_100`,`create_date`,`status`) VALUES ('$idaccount','$did_category', '$dname', '$dprice', '$dsale_off', '$dprice_sale_off', '$filePath1', '$filePath2', '$dtitle', '$dadvantage', '$dcondition', '$dcontent', '$freeship', '$delivery', '$top_seller', '$top_view', '$top_100',NOW(),0)");
			//return $result->result_array();
			//
		}
		
		public function list_deal1($cid)
		{
			if($cid==0)
			$result=$this->db->query("SELECT d.id,c.nameVN,d.`name`,d.price,d.sale_off,d.price_sale_off,d.status FROM deal d,category c WHERE c.id=d.id_category AND d.delete=0 AND d.status=1 ORDER BY d.create_date DESC");
			else 
			$result=$this->db->query("SELECT d.id,c.nameVN,d.`name`,d.price,d.sale_off,d.price_sale_off,d.status FROM deal d,category c WHERE c.id=d.id_category AND d.delete=0 AND d.status=1 AND (d.id_category='$cid' OR c.id_parent='$cid') ORDER BY d.create_date DESC");
			return $result->result_array();
		}
		
		public function list_deal($cid)
		{
			if($cid==0)
			$result=$this->db->query("SELECT d.id,c.nameVN,d.`name`,d.price,d.sale_off,d.price_sale_off,d.small_image,d.large_image,d.title,d.delivery,d.freeship,d.count,d.status  FROM deal d,category c WHERE c.id=d.id_category AND d.delete=0 AND d.status=1 ORDER BY d.create_date DESC");
			else 
			$result=$this->db->query("SELECT d.id,c.nameVN,d.`name`,d.price,d.sale_off,d.price_sale_off,d.small_image,d.large_image,d.title,d.delivery,d.freeship,d.count,d.status  FROM deal d,category c WHERE c.id=d.id_category AND d.delete=0 AND d.status=1 AND (d.id_category='$cid' OR c.id_parent='$cid') ORDER BY d.create_date DESC");
			return $result->result_array();
		}

		public function get_deal($did)
		{
			$result=$this->db->query("SELECT * FROM deal d WHERE d.id='$did'");
			return $result->result_array();
		}
		public function list_deal_top_100()
		{
			$result=$this->db->query("SELECT d.id,c.nameVN,d.`name`,d.price,d.sale_off,d.price_sale_off,d.small_image,d.large_image,d.title,d.delivery,d.freeship,d.count,d.status  FROM deal d,category c WHERE c.id=d.id_category AND d.delete=0 AND d.status=1 AND d.top_100=1 ORDER BY d.create_date DESC");
			return $result->result_array();
		}
		public function list_deal_today()
		{
			// AND d.top_view=1
			$result=$this->db->query("SELECT d.id,c.nameVN,d.`name`,d.price,d.sale_off,d.price_sale_off,d.small_image,d.large_image,d.title,d.delivery,d.freeship,d.count,d.status  
			FROM deal d,category c WHERE c.id=d.id_category AND d.delete=0 AND d.status=1 ORDER BY d.create_date DESC");
			return $result->result_array();
		}
		public function list_deal_top_seller()
		{
			$result=$this->db->query("SELECT d.id,c.nameVN,d.`name`,d.price,d.sale_off,d.price_sale_off,d.small_image,d.large_image,d.title,d.delivery,d.freeship,d.count,d.status  FROM deal d,category c WHERE c.id=d.id_category AND d.delete=0 AND d.top_seller=1 AND d.status=1 ORDER BY d.create_date DESC");
			return $result->result_array();
		}
		public function list_deal_search($m_namedeal)
		{
			$result=$this->db->query("SELECT d.id,c.nameVN,d.`name`,d.price,d.sale_off,d.price_sale_off,d.small_image,d.large_image,d.title,d.delivery,d.freeship,d.count,d.status FROM deal d,category c 
			WHERE c.id=d.id_category  AND d.delete=0  AND d.status=1 AND d.name like '%$m_namedeal%'
			ORDER BY d.create_date DESC");
			return $result->result_array();
		}
		
		public function list_deal_account($aid)
		{
			$result=$this->db->query("SELECT d.id,c.nameVN,d.`name`,d.price,d.sale_off,d.price_sale_off,d.small_image,d.large_image,d.title,d.delivery,d.freeship,d.count,d.status,a.`name` as aname
										FROM deal d,category c,account a 
										WHERE c.id=d.id_category AND d.id_account=a.id AND a.id='$aid'
										ORDER BY d.create_date DESC");
			return $result->result_array();
		}
		public function edit_deal($id_deal,$did_category,$dname,$dprice,$dsale_off,$dprice_sale_off,$filePath1,$filePath2,$dtitle,$dadvantage,$dcondition,$dcontent,$top_seller,$top_view,$top_100,$freeship,$delivery)
		{
			$result=$this->db->query("UPDATE `deal` SET `id_category`='$did_category', `name`='$dname', `price`='$dprice', `sale_off`='$dsale_off', `price_sale_off`='$dprice_sale_off', `small_image`='$filePath1', `large_image`='$filePath2', `title`='$dtitle', `advantage`='$dadvantage', `condition`='$dcondition',`detail`='$dcontent',`freeship`='$freeship', `delivery`='$delivery', `top_seller`='$top_seller', `top_view`='$top_view', `top_100`='$top_100' , `status`='0' WHERE (`id`='$id_deal')");
			//return $result->result_array();
			//
		}
		
	}
?>